/*
package com.secbro.drools;

import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.xpath.DefaultXPath;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

*/
/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2019-04-27
 * Time: 12:04
 * Des:
 *//*

public class DomParser {


    public static void main(String[] args) throws Exception {
        //1-获取XML-IO流
        InputStream xmlInputStream = getXmlInputStream("D:\\毕设\\新建文件夹\\Drools7规则引擎入门视频教程\\drools\\drools-lesson\\src\\main\\resources\\META-INF\\kmodule.xml");
        //2-解析XML-IO流 ，获取Document 对象，以及Document对象 的根节点
        org.w3c.dom.Element rootElement = getRootElementFromIs(xmlInputStream);
        //3~5-从根元素解析得到元素
        parseElementFromRoot(rootElement);

    }

    //1-获取XML-IO流
    private static InputStream  getXmlInputStream(String xmlPath){
        InputStream inputStream = null;
        try {
            //1-把要解析的 XML 文档转化为输入流，以便 DOM 解析器解析它
            inputStream= new FileInputStream(xmlPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputStream;
    }
    //2-解析XML-IO流 ，获取Document 对象，以及Document对象 的根节点
    private static org.w3c.dom.Element getRootElementFromIs(InputStream inputStream) throws Exception {
        if(inputStream == null){
            return  null;
        }
        */
/*
         * javax.xml.parsers 包中的DocumentBuilderFactory用于创建DOM模式的解析器对象 ，
         * DocumentBuilderFactory是一个抽象工厂类，它不能直接实例化，但该类提供了一个newInstance方法 ，
         * 这个方法会根据本地平台默认安装的解析器，自动创建一个工厂的对象并返回。
        *//*

        //2-调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //3-调用工厂对象的 newDocumentBuilder方法得到 DOM 解析器对象。
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        //4-调用 DOM 解析器对象的 parse() 方法解析 XML 文档，得到代表整个文档的 Document 对象，进行可以利用DOM特性对整个XML文档进行操作了。
        org.w3c.dom.Document doc = docBuilder.parse(inputStream);
        //5-得到 XML 文档的根节点
        org.w3c.dom.Element root =doc.getDocumentElement();
        //6-关闭流
        if(inputStream != null){
            inputStream.close();
        }
        return root;
    }

    //3-从根元素解析得到元素
    private static void parseElementFromRoot(org.w3c.dom.Element root) {
        NodeList nl = root.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            org.w3c.dom.Node node = nl.item(i);
            if (node instanceof org.w3c.dom.Element) {
                org.w3c.dom.Element ele = (org.w3c.dom.Element) node;
                //4-从元素解析得到属性值
                getDataFromElement(ele);
                //5-从元素解析特定子元素并解析(以property为例)
                getCertainElementFromParentElement(ele);
            }
        }
    }

    //4-从元素解析得到属性值
    private static void getDataFromElement(org.w3c.dom.Element ele) {
        String name = ele.getAttribute("name");//根据属性名称读取属性值
        System.out.println("name == " + name);
        String className = ele.getAttribute("class");
        System.out.println("className == " + className);
    }
    //5-从元素解析特定子元素并解析(以property为例)
    private static void getCertainElementFromParentElement(org.w3c.dom.Element ele) {
        NodeList propertyEleList = ele.getElementsByTagName("property");//根据标签名称获取标签元素列表
        for (int i = 0; i < propertyEleList.getLength(); i++) {
            org.w3c.dom.Node node = propertyEleList.item(i);
            if (node instanceof org.w3c.dom.Element) {
                org.w3c.dom.Element propertyEle = (org.w3c.dom.Element) node;
                String name = propertyEle.getAttribute("name");
                System.out.println("propertyEle: name == " + name);
                String value = propertyEle.getAttribute("value");
                System.out.println("propertyEle: value == " + value);
            }
        }

    }

    public static List<String> parseMethod1() throws DocumentException
    {
        List<String> result = new ArrayList<>();

        SAXReader reader = new SAXReader();
        Document document = reader.read("E:\\IdeaProjects\\Example\\NetApp\\src\\main\\resources\\target.xml");
        XPath xPath = new DefaultXPath("/resources/product[@name='QQ']/account[@id='987654321']/password");
        List<Element> list = xPath.selectNodes(document.getRootElement());
        for (Element element : list)
        {
            System.out.println(element.getTextTrim());
            result.add(element.getTextTrim());
        }
        return result;
    }

    public static List<String> parseMethod2() throws DocumentException
    {
        List<String> result = new ArrayList<String>();

        SAXReader reader = new SAXReader();
        Document document = reader.read("META-INF/kmodule.xml");
        System.out.println(document);
        return null;
     */
/*   List<Element> products = document.getRootElement().selectNodes("kbase");
        Iterator iterator = products.iterator();
        while (iterator.hasNext())
        {
            Element product = (Element) iterator.next();
            String name = product.attributeValue("name");
            System.out.println(name);
        }
        return result;*//*

    }

    public static void generate()
    {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("resources");

        Element product = root.addElement("product");
        product.addAttribute("name", "QQ");

        Element account = product.addElement("account");
        account.addAttribute("id", "123456789");

        Element nickname = account.addElement("nickname");
        nickname.setText("QQ-account-1");

        Element password = account.addElement("password");
        password.setText("123asd21qda");

        Element level = account.addElement("level");
        level.setText("56");

        PrintWriter pWriter = null;
        XMLWriter xWriter = null;
        try
        {
            pWriter = new PrintWriter("E:\\IdeaProjects\\Example\\NetApp\\src\\main\\generate.xml");
            xWriter = new XMLWriter(pWriter);
            xWriter.write(doc);
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                xWriter.flush();
                xWriter.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

}
*/
